<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/5
 * Time: 16:03
 */

namespace App\Http\Admin\Personnel\Models;

use Framework\BaseClass\Http\Model;
use App\Eloquent\Oa\Report as EloquentOaReport;


class Report extends Model
{

    public function getReportList($page, $pageSize, $searchField,$search_deportment,$search_user)
    {

        $list = EloquentOaReport::where($searchField)->orderBy('created_at','desc');
        if($search_deportment){
            $list->whereHas('deportmentInfo',function($query1)use($search_deportment){
                $query1->where('name','like','%'.$search_deportment.'%');
            });
        }else{
            $list->with('deportmentInfo');
        }

        if($search_user){
            $list->whereHas('contactslists',function($query2)use($search_user){
                $query2->where('name','like','%'.$search_user.'%');
            });
        }else{
            $list->with('deportmentInfo');
        }
        $count = $list->count();
        $list = $list->forPage($page, $pageSize)->get();

        foreach ($list as $v){

            $v->user_name=$v->contactslists? $v->contactslists->name : '';
            if($v['type']==1){
                $v['title']=date('Y-m-d',$v['time'])." 日报";
            }elseif($v['type']==2){
                $v['title']=date('Y-m-d',$v['time']).' 至 '.date('Y-m-d',$v['time']+60*60*24*7)." 周报";
            }elseif($v['type']==3){
                $v['title']=date('Y-m',$v['time'])." 月报";
            }
            $v['deportment']=$v->deportmentInfo ? $v->deportmentInfo->name : '';
            $v['created_date']=date('Y-m-d',$v['created_at']->timestamp);
        }

        $data['list'] = $list;
        $data['pageCurrent'] = $page;
        $data['totalRow'] = $count;
        return $data;
    }




    public function getReportInfo($id){
        $result = EloquentOaReport::where('id', '=', $id)->first();
        if (is_null($result)) xThrow(ERR_API_REPORT_NOT_EXIST);

            if($result['type']==1){
                $result['today']="今日完成工作：";
                $result['next']="明日工作计划：";
                $result['title']=date('Y-m-d',$result['time'])." 日报";
            }elseif($result['type']==2){
                $result['today']="本周完成工作：";
                $result['next']="下周工作计划：";
                $result['title']=date('Y-m-d',$result['time']).' 至 '.date('Y-m-d',$result['time']+60*60*24*7)." 周报";
            }elseif($result['type']==3){
                $result['today']="本月完成工作：";
                $result['next']="下月工作计划：";
                $result['title']=date('Y-m',$result['time'])." 月报";
            }
        $result['created_date']=date('Y-m-d',$result['created_at']->timestamp);
        switch ($result['done_tag'])
        {
            case 0:
                $done_tag="未知";
                break;
            case 1:
                $done_tag="一般";
                break;
            case 2:
                $done_tag="紧急";
                break;
            case 3:
                $done_tag="重要";
                break;
            case 4:
                $done_tag="临时";
                break;
            default:
                $done_tag="";
        }


        switch ($result['todo_tag'])
        {
            case 0:
                $todo_tag="未知";
                break;
            case 1:
                $todo_tag="一般";
                break;
            case 2:
                $todo_tag="紧急";
                break;
            case 3:
                $todo_tag="重要";
                break;
            case 4:
                $todo_tag="临时";
                break;
            default:
                $todo_tag="";
        }

        switch ($result['to_be_confirmed_tag'])
        {
            case 0:
                $to_be_confirmed_tag="未知";
                break;
            case 1:
                $to_be_confirmed_tag="一般";
                break;
            case 2:
                $to_be_confirmed_tag="紧急";
                break;
            case 3:
                $to_be_confirmed_tag="重要";
                break;
            case 4:
                $to_be_confirmed_tag="临时";
                break;
            default:
                $to_be_confirmed_tag="";
        }
        switch ($result['todo_next_tag'])
        {
            case 0:
                $todo_next_tag="未知";
                break;
            case 1:
                $todo_next_tag="一般";
                break;
            case 2:
                $todo_next_tag="紧急";
                break;
            case 3:
                $todo_next_tag="重要";
                break;
            case 4:
                $todo_next_tag="临时";
                break;
            default:
                $todo_next_tag="";
        }
        $result['done_tag']=$result['done']."(".$done_tag.")";
        $result['todo_tag']=$result['todo']."(".$todo_tag.")";
        $result['to_be_confirmed_tag']=$result['to_be_confirmed']."(".$to_be_confirmed_tag.")";
        $result['todo_next_tag']=$result['todo_next']."(".$todo_next_tag.")";
        return $result;
    }



}